package com.example.Sao.MyFragment;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v7.widget.CardView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.Toast;

import com.example.Sao.Adapter.ListAnimationAdpter;
import com.example.Sao.MyActivity.ChangeDeviceActivity;
import com.example.Sao.ObjectBox.DeviceEntity;
import com.example.Sao.R;
import com.example.Sao.Tools.ObjectBoxHelper;
import com.google.gson.Gson;

import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;

/**
 * @ProjectName: scan
 * @Package: com.example.MyApplication.MyActivity
 * @ClassName: InfoDiagFragment
 * @Description: 确认设备信息Fragment
 * @Author: 阿毛
 * @CreateDate: 2020/2/28 11:49
 * @UpdateUser: 更新者：
 * @UpdateDate: 2020/2/28 11:49
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public class ChangeInfoDiagFragment extends DialogFragment implements View.OnClickListener{
    private ObjectBoxHelper objectBoxHelper;
    private SuccesDialog.SuccesDialogLinster succesDialogLinster;
    private CardView iv_save,iv_change;
    private DeviceEntity lbeanList;
    private Context mContext;
    private RecyclerView list;
    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        return super.onCreateDialog(savedInstanceState);
    }
    public void setmContext(Context mContext) {
        this.mContext = mContext;
    }

    public void setLbeanList(DeviceEntity lbeanList) {
        this.lbeanList = lbeanList;
    }

    public void setSuccesDialogLinster(SuccesDialog.SuccesDialogLinster succesDialogLinster) {
        this.succesDialogLinster = succesDialogLinster;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            default:
                break;
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        objectBoxHelper = ObjectBoxHelper.getInstance();
        Window window = this.getDialog().getWindow();
        //去掉dialog默认的padding
        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        window.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = (int) (dm.widthPixels * 0.90);
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        //设置dialog的位置在底部
        lp.gravity = Gravity.CENTER;
        //设置dialog的动画
        lp.windowAnimations = R.style.dialogAnim;
        window.setAttributes(lp);
        window.setBackgroundDrawable(new ColorDrawable());
        View view = inflater.inflate(R.layout.fragment_info_layout,container,false);
        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        list = view.findViewById(R.id.RecyclerView);
        iv_change = view.findViewById(R.id.iv_change);
        iv_save = view.findViewById(R.id.iv_sure);
        iv_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (lbeanList.isStaus()) {
                    Toast.makeText(mContext,"该设备已被盘点",Toast.LENGTH_LONG).show();
                }else {
                    lbeanList.setStaus(true);
                    objectBoxHelper.add(lbeanList);
                }
                if (succesDialogLinster!=null){
                    succesDialogLinster.SuccesDialogLinster(true);
                }
                ChangeInfoDiagFragment.this.dismiss();
            }
        });
        iv_change.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent =  new Intent(mContext, ChangeDeviceActivity.class);
                intent.setFlags(FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra("devicemess",new Gson().toJson(lbeanList));
                mContext.startActivity(intent);
                ChangeInfoDiagFragment.this.dismiss();
            }
        });
        initAnim();
        if (lbeanList!=null){
            list.setLayoutManager(new LinearLayoutManager(mContext));
            ListAnimationAdpter listAnimationAdpter = new ListAnimationAdpter(mContext,lbeanList);
            list.setAdapter(listAnimationAdpter);
        }
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    //设置动画效果
    private void initAnim() {
        //通过加载XML动画设置文件来创建一个Animation对象；
        Animation animation = AnimationUtils.loadAnimation(getContext(), R.anim.item_anim);
        //得到一个LayoutAnimationController对象；
        LayoutAnimationController lac = new LayoutAnimationController(animation);
        //设置控件显示的顺序；
        lac.setOrder(LayoutAnimationController.ORDER_NORMAL);
        //设置控件显示间隔时间；
        lac.setDelay(0.3f);
        //为ListView设置LayoutAnimationController属性；
        list.setLayoutAnimation(lac);
    }
    public interface SuccesDialogLinster{
        public void SuccesDialogLinster(boolean isSucces);
    }
}